﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.IO.Compression;
using System.IO;
using System.Web.UI;
using System.Configuration;
using BAL;

namespace ProjectQuaDep
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpCompress((HttpApplication)sender);
        }


        private void HttpCompress(HttpApplication app)
        {
            string acceptEncoding = app.Request.Headers["Accept-Encoding"];
            Stream prevUncompressedStream = app.Response.Filter;


            if (!(app.Context.CurrentHandler is Page) ||
                app.Request["HTTP_X_MICROSOFTAJAX"] != null)
                return;


            if (string.IsNullOrEmpty(acceptEncoding))
                return;


            acceptEncoding = acceptEncoding.ToLower();


            if ((acceptEncoding.Contains("deflate") || acceptEncoding == "*")
                && CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
            {
                // deflate 
                app.Response.Filter = new DeflateStream(prevUncompressedStream,
                    CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "deflate");
            }
            else if (acceptEncoding.Contains("gzip")
                && CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
            {
                // gzip 
                app.Response.Filter = new GZipStream(prevUncompressedStream,
                    CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "gzip");
            }
        }


        private static bool CompressScript(string scriptName)
        {
            if (scriptName.ToLower().Contains(".axd")) return false;
            return true;
        } 
        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError().GetBaseException();
            string WriteErrorToFile = ConfigurationManager.AppSettings["WriteErrorToFile"];
            if (WriteErrorToFile != null && WriteErrorToFile.ToUpper() == "TRUE")
            {
                Utils.CreateLogFiles Err = new Utils.CreateLogFiles();
                Err.ErrorLog(Server.MapPath("/Upload"), Environment.NewLine + Environment.NewLine + Request.RawUrl.ToString() + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace);
            }
            //if (HttpContext.Current.Request.Path.ToLower() != "/default.aspx"
            //    && HttpContext.Current.Request.Path.ToLower() != "/home.htm"
            //    && HttpContext.Current.Request.Path.ToLower() != "/home.aspx") Response.Redirect("/home.htm");
            //else Response.Redirect("/error.html");
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}